﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hanhud
{
    public class CPhotoCategory : ICategory<int, CPhotoCategory>
    {
        private int _photoCategoryID = -1;

        private string _title = string.Empty;
        public string Title { get { return _title; } set { _title = value; } }

        public string _description = string.Empty;
        public string Description { get { return _description; } set { _description = value; } }

        private bool _published = false;
        public bool Published { get { return _published; } set { _published = value; } }

        public CPhotoCategory()
        {
        }

        public CPhotoCategory(int photoCategoryID)
        {
            this.CategoryID = photoCategoryID;
        }

        public CPhotoCategory(int photoCategoryID, string title, string description)
        {
            this.CategoryID = photoCategoryID;
            this.Title = title;
            this.Description = description;
        }

        public bool Update()
        {
            bool success = false;
            return success;
        }

        public int CategoryID
        {
            get { return _photoCategoryID; }
            protected set { _photoCategoryID = value; }
        }

        private ICategory<int, CPhotoCategory> _parentCategory = null;
        public ICategory<int, CPhotoCategory> ParentCategory
        {
            get
            {
                return _parentCategory;
            }
            set
            {
                _parentCategory = value;
            }
        }

        private List<ICategory<int, CPhotoCategory>> _childCategories = null;
        public List<ICategory<int, CPhotoCategory>> ChildCategories
        {
            get
            {
                if (_childCategories == null)
                {
                    _childCategories = new List<ICategory<int, CPhotoCategory>>();
                }
                return _childCategories;
            }
            protected set { _childCategories = value; }
        }

        public string EncodedTitle
        {
            get { return _title; }
        }

        public CPhotoCategory OfType
        {
            get { return this; }
        }

        #region Public static methods
        public static CPhotoCategory GetCategory(int categoryID)
        {
            CPhotoCategory category = Data.Providers.CMediaProvider.Instance.GetCategory(categoryID);
            if (category != null)
                category.ChildCategories.AddRange(GetCategoryTree(category.CategoryID));

            return category;
        }

        public List<ICategory<int, CPhotoCategory>> GetCategoryTree()
        {
            return CPhotoCategory.GetCategoryTree(this.CategoryID);
        }

        public static List<ICategory<int, CPhotoCategory>> GetCategoryTree(int parentCategoryID)
        {
            List<ICategory<int, CPhotoCategory>> categoryTree = new List<ICategory<int, CPhotoCategory>>();

            foreach (ICategory<int, CPhotoCategory> category in Data.Providers.CMediaProvider.Instance.GetAllCategories(parentCategoryID))
            {
                if (category.OfType.ParentCategory == null)
                    categoryTree.Add(category);
                else
                {
                    ICategory<int, CPhotoCategory> parent = Find(categoryTree, new CPhotoCategory(category.OfType.ParentCategory.CategoryID));
                    if (parent == null) throw new InvalidOperationException();

                    parent.ChildCategories.Add(category);
                    category.ParentCategory = parent;
                }
            }

            return categoryTree;
        }

        public static IEnumerable<CPhotoCategory> GetCategories()
        {
            return GetCategories(-1);
        }
        public static List<CPhotoCategory> GetCategories(int parentCategoryID)
        {
            return null;
            //List<CPhotoCategory> categoryList = new List<CPhotoCategory>(Providers.CStoreProvider.Instance.GetCategories(parentCategoryID));

            //return categoryList;
        }

        public static CPhotoCategory CreateCategory(int photoCategoryID, string title, string description)
        {
            //int categoryID = Providers.CStoreProvider.Instance.CreateCategory(photoCategoryID, title, description);

            //if (categoryID > 0)
            //    return CPhotoCategory.GetCategory(categoryID);
            //else
            //    return null;
            return null;
        }
        
        public static bool UpdateCategory(CPhotoCategory category)
        {
            bool success = false;
            ////success = UpdateCategory(category.CategoryID, category.ParentCategory == null ? 0 : category.ParentCategory.CategoryID, category.Title, category.Description);
            //success = Providers.CStoreProvider.Instance.UpdateCategory(
            //        category.CategoryID,
            //        category.ParentCategory == null ? 0 : category.ParentCategory.CategoryID,
            //        category.Title,
            //        category.Description
            //    );
            //if (success)
            //{
            //    // Clear cache
            //}
            return success;
        }

        #endregion

        private static ICategory<int, CPhotoCategory> Find(List<ICategory<int, CPhotoCategory>> categoryList, ICategory<int, CPhotoCategory> match)
        {
            ICategory<int, CPhotoCategory> foundCategory = null;

            foreach (ICategory<int, CPhotoCategory> category in categoryList)
            {
                if (category.CategoryID == match.CategoryID)
                {
                    foundCategory = category;
                    break;
                }
                else if (category.ChildCategories != null && category.ChildCategories.Count > 0)
                {
                    if ((foundCategory = Find(category.ChildCategories, match)) != null)
                        break;
                }
            }

            return foundCategory;
        }
    }
}
